Skip to content

fix TypeVar with constraints not resolving __getitem__ #3059 - #3358

Closed
asukaminato0721 wants to merge 1 commit into
pyrefly-org:mainfrom
asukaminato0721:3059
Closed

asukaminato0721 wants to merge 1 commit into
pyrefly-org:mainfrom
asukaminato0721:3059

Conversation

@asukaminato0721

@asukaminato0721 asukaminato0721 commented May 11, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #3059

restricted TypeVar subscripting uses the bound or constraint types directly, which reuses the existing precise TypedDict indexing path instead of falling back to object.__getitem__.

Test Plan

add test

@github-actions

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
- ERROR homeassistant/components/bluetooth/match.py:200:54-73: Cannot index into `_T` [bad-index]
- ERROR homeassistant/components/bluetooth/match.py:207:34-58: Cannot index into `_T` [bad-index]
- ERROR homeassistant/components/bluetooth/match.py:211:31-52: Cannot index into `_T` [bad-index]
- ERROR homeassistant/components/bluetooth/match.py:215:36-62: Cannot index into `_T` [bad-index]
- ERROR homeassistant/components/bluetooth/match.py:227:54-73: Cannot index into `_T` [bad-index]
- ERROR homeassistant/components/bluetooth/match.py:233:34-58: Cannot index into `_T` [bad-index]
- ERROR homeassistant/components/bluetooth/match.py:237:31-52: Cannot index into `_T` [bad-index]
- ERROR homeassistant/components/bluetooth/match.py:241:36-62: Cannot index into `_T` [bad-index]
- ERROR homeassistant/data_entry_flow.py:327:48-329:14: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:333:29-43: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:376:12-28: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:382:23-42: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:385:12-28: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:389:69-391:14: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:399:69-401:14: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:415:16-35: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:416:17-31: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:418:21-48: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:419:24-60: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:494:16-30: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:502:27-41: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:536:54-71: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/data_entry_flow.py:553:12-26: Cannot index into `_FlowResultT` [bad-index]
- ERROR homeassistant/helpers/collection.py:269:21-41: Cannot index into `_StoreT` [bad-index]
- ERROR homeassistant/helpers/collection.py:277:29-49: Cannot index into `_StoreT` [bad-index]
- ERROR homeassistant/helpers/event.py:351:45-68: Cannot index into `_StateEventDataT` [bad-index]
- ERROR homeassistant/helpers/event.py:359:17-40: Cannot index into `_StateEventDataT` [bad-index]
- ERROR homeassistant/helpers/event.py:371:12-35: Cannot index into `_StateEventDataT` [bad-index]

@github-actions

Copy link
Copy Markdown

Primer Diff Classification

✅ 1 improvement(s) | 1 project(s) total | -28 errors

1 improvement(s) across core.

Project Verdict Changes Error Kinds Root Cause
core ✅ Improvement -28 bad-index pyrefly/lib/alt/expr.rs
Detailed analysis

✅ Improvement (1)

core (-28)

This is a clear bug fix in pyrefly. The class BluetoothMatcherIndexBase uses a constrained TypeVar _T: (BluetoothMatcher, BluetoothCallbackMatcherWithCallback) where both constraints are TypedDict subclasses. TypedDicts support __getitem__ (dictionary-style indexing), so expressions like matcher[LOCAL_NAME] and matcher[MANUFACTURER_ID] are valid for all possible values of _T. The old behavior incorrectly reported 28 bad-index errors ("Cannot index into _T") across 4 files in the Home Assistant codebase because pyrefly did not recognize that when all constraints of a constrained TypeVar support indexing, the TypeVar itself should also support indexing. The new test case test_constrained_type_var_typed_dict_index in the PR confirms this was an intentional fix for issue #3059.
Attribution: The change in pyrefly/lib/alt/expr.rs in the subscript_infer_for_type match arm for Type::Quantified with restricted TypeVars is the direct cause. Previously, it tried call_method_or_error with __getitem__ on the raw TypeVar type, which failed. Now it dispatches to subscript_infer_for_type on each constraint (for Restriction::Constraints) or the bound (for Restriction::Bound), correctly resolving the indexing operation through the actual types.


Was this helpful? React with 👍 or 👎

Classification by primer-classifier (1 LLM)

@asukaminato0721
asukaminato0721 marked this pull request as ready for review May 11, 2026 15:27
Copilot AI review requested due to automatic review settings May 11, 2026 15:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a false-positive when subscripting a constrained TypeVar whose constraints are TypedDict types (issue #3059) by teaching subscript inference to resolve indexing against the TypeVar’s bound/constraints rather than attempting __getitem__ directly on the TypeVar.

Changes:

  • Update subscript_infer_for_type to handle restricted TypeVars by delegating subscript inference to the TypeVar’s bound or to the union of its constraints.
  • Add a regression test covering constrained TypeVar + TypedDict indexing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pyrefly/lib/alt/expr.rs Adjusts subscript inference for restricted TypeVars to use bound/constraints so TypedDict subscripting resolves correctly.
pyrefly/lib/test/typed_dict.rs Adds a regression test reproducing #3059 and asserting the inferred type for device[NAME].

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

assert_type(name, str)
self.by_name.setdefault(name, []).append(device)

def lookup(self, device: T) -> str:
@meta-codesync

meta-codesync Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D107101498.

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in 4b40b3c Jun 4, 2026
@meta-codesync

meta-codesync Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

@yangdanny97 merged this pull request in 4b40b3c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeVar with constraints not resolving __getitem__

3 participants